package com.lzy.cloud.goods.config;

import com.lzy.framework.core.datasource.DatasourceProperties;
import com.lzy.framework.core.utils.cryptors.AESCryptor;
import com.lzy.framework.druid.pool.DruidDataSource;
import com.lzy.framework.druid.support.http.StatViewServlet;
import com.lzy.framework.druid.support.http.WebStatFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import java.sql.SQLException;

/**
 * Created with IntelliJ IDEA.
 * Description: lzy-cloud-service
 *
 * @author: ellengou
 * @date: 2018-05-22 星期二
 * @time: 12:37
 */
@Configuration
@EnableConfigurationProperties(DatasourceProperties.class)
public class DataSourceConfig {

    private Logger logger = LoggerFactory.getLogger(DataSourceConfig.class);

    @Autowired
    DatasourceProperties datasourceProperties;

    @Bean
    public ServletRegistrationBean druidServlet() {
        ServletRegistrationBean reg = new ServletRegistrationBean();
        reg.setServlet(new StatViewServlet());
        reg.addUrlMappings("/druid/*");
        reg.addInitParameter("loginPassword", AESCryptor.getInstance().decrypt(datasourceProperties.getPassword()));
        reg.addInitParameter("loginUsername", AESCryptor.getInstance().decrypt(datasourceProperties.getUsername()));
        return reg;
    }

    @Bean
    public FilterRegistrationBean filterRegistrationBean() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.setFilter(new WebStatFilter());
        filterRegistrationBean.addInitParameter("profileEnable", "true");
        filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        return filterRegistrationBean;
    }

    @Bean
    @Primary
    public DruidDataSource druidDataSource() {
        DruidDataSource datasource = new DruidDataSource();
        datasource.setUsername(datasourceProperties.getUsername());
        datasource.setUrl(datasourceProperties.getUrl());
        datasource.setPassword(datasourceProperties.getPassword());
        datasource.setDriverClassName(datasourceProperties.getDriverClassName());
        datasource.setInitialSize(datasourceProperties.getInitialSize());
        datasource.setMinIdle(datasourceProperties.getMinIdle());
        datasource.setMaxActive(datasourceProperties.getMaxActive());
        datasource.setMaxWait(datasourceProperties.getMaxWait());
        datasource.setTimeBetweenEvictionRunsMillis(datasourceProperties.getTimeBetweenEvictionRunsMillis());
        datasource.setMinEvictableIdleTimeMillis(datasourceProperties.getMinEvictableIdleTimeMillis());
        datasource.setValidationQuery(datasourceProperties.getValidationQuery());
        datasource.setTestWhileIdle(datasourceProperties.getTestWhileIdle());
        datasource.setTestOnBorrow(datasourceProperties.getTestOnBorrow());
        datasource.setTestOnReturn(datasourceProperties.getTestOnReturn());
        try {
            datasource.setFilters(datasourceProperties.getFilters());
        } catch (SQLException e) {
            logger.error("druid configuration initialization filter", e);
        }
        return datasource;
    }
}